package com.zwd.deep.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwd.deep.common.IntegerEnum;
import com.zwd.deep.common.ResultEnum;
import com.zwd.deep.entity.dto.DoctorsPageDTO;
import com.zwd.deep.entity.dto.UsersDTO;
import com.zwd.deep.entity.dto.UsersLoginDto;
import com.zwd.deep.entity.dto.UsersPageDTO;
import com.zwd.deep.entity.po.Doctors;
import com.zwd.deep.entity.po.Users;
import com.zwd.deep.entity.vo.DoctorsVo;
import com.zwd.deep.entity.vo.UsersVo;
import com.zwd.deep.handle.exception.GlobalException;
import com.zwd.deep.mapper.UsersMapper;
import com.zwd.deep.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwd.deep.utils.HttpServletUtil;
import com.zwd.deep.utils.JwtUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author zwd
 * @since 2025-02-12
 */
@Service
@AllArgsConstructor
@Slf4j
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {
    private final UsersMapper usersMapper;

    @Override
    public String login(UsersLoginDto usersLoginDto) {
        Users users = usersMapper.selectByPhone(usersLoginDto.getPhone());
        String md5Hex = DigestUtil.md5Hex(usersLoginDto.getPassword());
        if (Objects.isNull(users) || !Objects.equals(users.getPassword(), md5Hex)) {
            throw new GlobalException(ResultEnum.USER_OR_PASSWORD_ERROR);
        }
        HashMap<Object, Object> claims = new HashMap<>();
        claims.put("id", users.getUserId());
        String token = JwtUtil.generateToken(claims);
        return token;
    }

    @Override
    public UsersVo register(UsersDTO usersDTO) {
        Users selectByPhone = usersMapper.selectByPhone(usersDTO.getPhone());
        if (Objects.nonNull(selectByPhone)) {
            throw new GlobalException(ResultEnum.USERNAME_EXIST);
        }
        Users users = new Users();
        BeanUtil.copyProperties(usersDTO, users);
        users.setPassword(DigestUtil.md5Hex(users.getPassword()));
        if(!Objects.isNull(usersDTO.getAvatar())){
            users.setAvatar(usersDTO.getAvatar());
        }
        users.setGender(IntegerEnum.ONE);
        int rows = usersMapper.insert(users);
        if (rows <= IntegerEnum.ZERO) {
            throw new GlobalException(ResultEnum.REGISTER_FAIL);
        }
        UsersVo usersVo = new UsersVo();
        BeanUtil.copyProperties(users, usersVo);
        return usersVo;
    }

    @Override
    public UsersVo getByUserId(String userId) {
        Users users = usersMapper.selectById(userId);
        if (Objects.isNull(users)) {
            throw new GlobalException(ResultEnum.USER_NOT_EXIST);
        }
        UsersVo usersVo = new UsersVo();
        BeanUtil.copyProperties(users, usersVo);
        return usersVo;
    }

    @Override
    public boolean updateByUserId(UsersDTO usersDTO) {
        Users users = new Users();
        users.setUserId(Long.valueOf(JwtUtil.extractUserId(HttpServletUtil.getToken())));
        BeanUtil.copyProperties(usersDTO, users);
        int rows = usersMapper.updateById(users);
        if (rows < IntegerEnum.ZERO) {
            throw new GlobalException(ResultEnum.UPDATE_FAIL);
        }
        return true;
    }

    @Override
    public List<UsersVo> getPage(UsersPageDTO usersPageDTO) {
        Page<Users> page = new Page<>(usersPageDTO.getPage(), usersPageDTO.getSize());
        Page<Users> usersPage = usersMapper.selectPage(page, null);
        List<UsersVo> usersVos = BeanUtil.copyToList(usersPage.getRecords(), UsersVo.class);
        return usersVos;
    }

}
